/////////////////////////////////////////////////////////
//                                                     //
// Copyright (c) 2016年 上证云平台. All rights reserved. //
//                                                     //
/////////////////////////////////////////////////////////

#import "FundValueViewController.h"
#if __has_include("MChartView.h")

#import "MChartView.h"
#import "MApi.h"

@interface FundValueViewController ()
@property (nonatomic, strong) MFundChartView *chartView;
@property (nonatomic, weak) NSTimer *timer;
@end

@implementation FundValueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    
    self.chartView = [[MFundChartView alloc] initWithFrame:CGRectZero];
    self.chartView.code = @"502025.sh";
    //    self.chartView.gradientColors = @[[[UIColor redColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor orangeColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor yellowColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor greenColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor blueColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor magentaColor] colorWithAlphaComponent:0.3],
    //                                      [[UIColor purpleColor] colorWithAlphaComponent:0.3]
    //                                      ];
    //    self.chartView.gradientLocations = (CGFloat []){ 0.0, 0.9, 1.0 };
    
    //    self.chartView.yAxisLabelInside = NO;
    //    self.chartView.yAxisFont = [UIFont boldSystemFontOfSize:17];
    //    self.chartView.borderWidth = 5;
    //    self.chartView.borderColor = [UIColor whiteColor];
    //    self.chartView.insideLineWidth = 2;
    //    self.chartView.insideLineColor = [UIColor purpleColor];
    //    self.chartView.xAxisFont = [UIFont boldSystemFontOfSize:12.0];
    //    self.chartView.xAxisTextColor = [UIColor redColor];
    //    self.chartView.priceRiseTextColor = [UIColor yellowColor];
    //    self.chartView.priceFlatTextColor = [UIColor greenColor];
    //    self.chartView.priceDropTextColor = [UIColor brownColor];
    //    self.chartView.rangeRiseTextColor = [UIColor orangeColor];
    //    self.chartView.rangeFlatTextColor = [UIColor purpleColor];
    //    self.chartView.rangeDropTextColor = [UIColor redColor];
    //    self.chartView.currentLineWidth = 5;
    //    self.chartView.currentLineColor = [UIColor blueColor];
    //    self.chartView.startTime = @"2015-09-10";
    //    self.chartView.endTime = @"2016-10-11";

    self.chartView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin| UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.chartView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startEnquiry:) name:MChartDidStartEnquiryNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEnquiry:) name:MChartDidEndEnquiryNotification object:nil];
    
    
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.chartView.frame = CGRectMake(5, 5, CGRectGetWidth(self.view.frame) - 10, 300);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(fetch) userInfo:nil repeats:YES];
    [self.timer fire];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
}

#pragma mark - private
- (void)fetch {
    [self.chartView reloadData];
}

- (void)startEnquiry:(NSNotification *)notification {
    NSDictionary *item = notification.object;
    NSString *string = [NSString stringWithFormat:@"%@:%@",
                        item[@"UnitNAV"],
                        item[@"ENDDATE"]];
    self.navigationItem.title = string;
}

- (void)endEnquiry:(NSNotification *)notification {
    self.navigationItem.title = nil;
}

@end

#endif